package com.test.selenium.util;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import junit.framework.TestCase;

import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.test.selenium.AirSmokeTest;
//import com.test.selenium.ManualInvoiceTest;
import com.test.selenium.data.OutputSheet;
import com.test.selenium.data.Results;
import com.test.selenium.data.UTiLogin;


public class ManualInvoice extends TestCase{
	
	private ManualInput mtestSheet;
	private WebDriver driver;
	private WebDriverWait wait;
	private String manualUrl;
	SmokeTestTimeStamp timeStamp;
	private OutputSheet resultSheet;
	private long timeBeforeSave;
	private long timeAfterSave;
	private double testTimeTakenSec;
	private String isPass;
	private String rStatus;
	
	public ManualInvoice() {
			}
	
	public String createUniqueItemNbr()
	{
	  	String itemNbrStr;
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(1000);
		itemNbrStr = (randomInt+"ITM");
	    return itemNbrStr;
		
	}
	public  ManualInvoice (WebDriver driver, SmokeTestTimeStamp timeStamp, ManualInput mtestSheet,OutputSheet resultSheet ){
		
		this.driver = driver;
		this.timeStamp = timeStamp;
		this.mtestSheet = mtestSheet;
		this.resultSheet = resultSheet;
		manualUrl = UTiLogin.url + "#!manual_invoice";
		wait = new WebDriverWait(driver, 60);
		

}

	
	public Results createManualInvoice() throws InterruptedException, IOException{
		ManualInvoice mInvoice = new ManualInvoice();
		driver.get(manualUrl);
		WebElement productGroupDropDown= wait.until(ExpectedConditions.elementToBeClickable(By.id("product_group_code")));
	        new Select (productGroupDropDown).selectByVisibleText(mtestSheet.getProductGroup());
	    driver.findElement(By.id("payer_account_number")).sendKeys(mtestSheet.getPayer());  
	    driver.findElement(By.id("manual_invoice_oracle_billing_site_code")).sendKeys(mtestSheet.getBillingSite());
	    driver.findElement(By.id("transaction_type_business_number")).sendKeys(mInvoice.createUniqueItemNbr());
	    driver.findElement(By.id("transaction_type_business_number")).sendKeys(Keys.TAB);
	    Thread.sleep(3000);
	    driver.findElement(By.id("additional_date")).sendKeys(timeStamp.getArrivalDate2());
	    driver.findElement(By.id("additional_date")).sendKeys(Keys.TAB);
	    Thread.sleep(4000);
	    WebElement productCodeDropDown= wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[4]/div/div/form/div[2]/div[2]/ul/li/select")));
	        new Select (productCodeDropDown).selectByVisibleText("NON-TRANSPORTATION");
	        Thread.sleep(4000);
	        
	        timeBeforeSave = System.currentTimeMillis();
	        
	    //Click Add button to save Manual Invoice Item
	    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("add_invoice"))).click();
	    
	    timeAfterSave = System.currentTimeMillis();
        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
        
        
        
        if (driver.findElement(By.id("0")) != null){
        	isPass = "PASS";
        }
        else{
        	isPass = "FAIL";
        }
        	
       
        Results manualResults = new Results();
        manualResults.setArea("ManualInvoice");
        manualResults.setModule("Create Manual Invoice");
        manualResults.setValue(mInvoice.createUniqueItemNbr());
        manualResults.setStatus(isPass);
        manualResults.setTimeTaken(Double.toString(testTimeTakenSec));
        
        return manualResults;
        
      //(shipmentResults);
	    
	 // resultSheet.writeTestStepResult(++ManualInvoiceTest.TESTSTEP,"Manual Invoice", "Create Manual Invoice", true, testTimeTakenSec, "Item Number :"  + mInvoice.createUniqueItemNbr());
	  
	  
	  // long timeBeforeSave = System.currentTimeMillis();
	}
	//Add charges to Manual invoice item
	public Results addchargesManualInvoice() throws InterruptedException, IOException{
		Thread.sleep(7000);
		Actions action = new Actions(driver);
		driver.findElement(By.id("0")).click();
		driver.findElement(By.linkText("Charges")).click();
		Thread.sleep(8000);
		driver.findElement(By.id("charge_code")).sendKeys(mtestSheet.getChargecode2());
		driver.findElement(By.id("charge_code")).sendKeys(Keys.TAB);
		new Select (wait.until(ExpectedConditions.elementToBeClickable(By.id("basis")))).selectByVisibleText("LUMP SUM");
		Thread.sleep(5000);
		driver.findElement(By.id("transaction_charge_rates_rate_amount_sell")).sendKeys(mtestSheet.getAmount());
		driver.findElement(By.id("transaction_charge_rates_rate_amount_currency_code_sell")).sendKeys(mtestSheet.getCurrency());
		driver.findElement(By.id("transaction_charge_rates_rate_amount_currency_code_sell")).sendKeys(Keys.TAB);
		Thread.sleep(3000);
			
		driver.findElement(By.id("costAdd")).click();
		
		Thread.sleep(4000);
		
		timeBeforeSave = System.currentTimeMillis();
		wait.until(ExpectedConditions.elementToBeClickable(By.id("costSave"))).click();
		
		timeAfterSave = System.currentTimeMillis();
        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
        
       // WebElement table = driver.findElement(By.id("billing_charges_grid"));
        
        
       if (driver.findElement(By.id("0"))!= null)
       {
    	   isPass = "PASS";
       }
       else
       {
    	   isPass = "FAIL";
       }
        
        // resultSheet.writeTestStepResult(++ManualInvoiceTest.TESTSTEP,"Manual Invoice", " Add Charges", true, testTimeTakenSec );
        
        Results manualResults = new Results();
        manualResults.setArea("ManualInvoice");
        manualResults.setModule("Add Manual Invoice Charges");
        manualResults.setValue(" ");
        manualResults.setStatus(isPass);
        manualResults.setTimeTaken(Double.toString(testTimeTakenSec));
        
        Thread.sleep(12000);
        
		
        
        
        return manualResults;
	}
	
	public Results releaseChargesManualInvoice() throws InterruptedException, IOException{
		Thread.sleep(4000);
		driver.findElement(By.id("close_charges_button")).click();
		
		timeBeforeSave = System.currentTimeMillis();
		
		driver.findElement(By.id("release_invoice")).click();
		timeAfterSave = System.currentTimeMillis();
        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
        Thread.sleep(5000);
        if (driver.findElement(By.id("release_invoice")).isEnabled())
        {
        	isPass = "FAIL";
        }
        else
        {
        	isPass = "PASS";
        }
        
        Results manualResults = new Results();
        manualResults.setArea("ManualInvoice");
        manualResults.setModule("Release Manual Invoice Charges");
        manualResults.setValue(" ");
        manualResults.setStatus(isPass);
        manualResults.setTimeTaken(Double.toString(testTimeTakenSec));
        
       // resultSheet.writeTestStepResult(++ManualInvoiceTest.TESTSTEP,"Manual Invoice", "Release Charges", true, testTimeTakenSec );
		
        Thread.sleep(10000);
		driver.findElement(By.id("unrelease_invoice")).click();
		Thread.sleep(8000);
		return manualResults;
	}
	
	public Results demandChargesManualInvoice() throws InterruptedException, IOException{
		Thread.sleep(4000);
		driver.findElement(By.id("finalize_invoice")).click();
		driver.findElement(By.id("printer_code")).sendKeys("000000052");
		//driver.findElement(By.id("printer_code")).sendKeys(Keys.TAB);
		timeAfterSave = System.currentTimeMillis();
		
		driver.findElement(By.id("uti-printer-print")).click();
		Thread.sleep(2000);
			
		driver.findElement(By.id("uti-printer-print")).click();
		
		timeAfterSave = System.currentTimeMillis();
        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
        Thread.sleep(4000);
        if (driver.findElement(By.id("finalize_invoice")).isEnabled())
        {
        	isPass = "FAIL";
        }
        else
        {
        	isPass = "PASS";
        }
        
        Results manualResults = new Results();
        manualResults.setArea("ManualInvoice");
        manualResults.setModule("Finalize Manual Invoice Charges");
        manualResults.setValue(" ");
        manualResults.setStatus(isPass);
        manualResults.setTimeTaken(Double.toString(testTimeTakenSec));
        
        
       // resultSheet.writeTestStepResult(++ManualInvoiceTest.TESTSTEP,"Manual Invoice", "Demand Charges", true, testTimeTakenSec );
		Thread.sleep(10000);
	
		return manualResults;
	}
}
